package com.ymatou.iapi.productmanager.testcase;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.parameter.activityProduct.ActivityProductDto;
import com.ymatou.iapi.productmanager.parameter.activityProduct.GetActivityAllProductsResponse;
import com.ymatou.iapi.productmanager.service.GetActivityAllProductsCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.business.service.productdata.impl.ProductFactory;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;


public class Ts_GetActivityAllProducts {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static IntergratedProductMapper intergratedProductMapper = SqlMapperFactory.getIntergratedProductMapper();
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取直播中商品");
	}
	
	@Before
	public void caseUp() {
		
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetActivityAllProductsCall().getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityAllProducts_001() {
		Logger.start(true,"获取直播中商品");
		try{
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 5).stream()
											.map(x->x.getProductId()).collect(Collectors.toList());
			
			JSONObject retBean = new GetActivityAllProductsCall().call(sellerId, liveId);
			
			checkGetActivityAllProducts(retBean,productIds);
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityAllProducts_002() {
		Logger.start(true,"移出商品后,获取直播中商品");
		try{
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 5).stream()
											.map(x->x.getProductId()).collect(Collectors.toList());
		
			SellerProductCallService.removeProductFromLive(sellerId, liveId, productIds);
			
			JSONObject retBean = new GetActivityAllProductsCall().call(sellerId, liveId);
			
			Logger.verifyEquals(true, retBean.getJSONObject("Data").isNull("Products"), "移出商品后,获取直播中商品");
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetActivityAllProducts_003() {
		Logger.start(true,"获取直播中商品 直播或sellerId不存在");
		try{
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			List<String> productIds = ProductFactory.getProductWithExistStartLive(sellerId, sellerName, liveId, 5).stream()
											.map(x->x.getProductId()).collect(Collectors.toList());
			
			JSONObject retBean = new GetActivityAllProductsCall().call(sellerId, liveId);
			
			checkGetActivityAllProducts(retBean,productIds);
			
			
			JSONObject retBean1 = new GetActivityAllProductsCall().call(sellerId+1, liveId);
			
			Logger.verifyEquals(true, retBean1.getJSONObject("Data").isNull("Products"), "获取直播中商品 直播或sellerId不存在");
			
			
			JSONObject retBean2 = new GetActivityAllProductsCall().call(sellerId, liveId+100);
			
			Logger.verifyEquals(true, retBean2.getJSONObject("Data").isNull("Products"), "获取直播中商品 直播或sellerId不存在");
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
		
	}
	
	
	
	
	public static void checkGetActivityAllProducts(JSONObject ret, List<String> tgtProdIdList) throws Exception{
		Logger.verifyEquals(200, ret.getInt("Code"), "返回码检查");
		
		GetActivityAllProductsResponse actBean = ProductManagerCallService.getBeanFromJson(ret.getJSONObject("Data"), GetActivityAllProductsResponse.class);
		GetActivityAllProductsResponse tgtBean = getGetActivityAllProductsResponse(tgtProdIdList);
		
		AssertHelper.assertResultEqual(tgtBean, actBean, "checkGetActivityAllProducts");
		
		
	}
	
	
	public static GetActivityAllProductsResponse getGetActivityAllProductsResponse(List<String> prodIdList){
		GetActivityAllProductsResponse retBean = new GetActivityAllProductsResponse();
		List<ActivityProductDto> productsDto = new ArrayList<>();
		
		for (String prodId : prodIdList){
			Map<String,Object> retInSql = intergratedProductMapper.selectProductByProdId(prodId);
			List<Map<String,Object>> catalogsInSql = intergratedProductMapper.selectCatalogsByProdId(prodId);
			
			ActivityProductDto apd = new ActivityProductDto();
			apd.setProductId(prodId);
			apd.setProductName(retInSql.get("sProduct").toString());
			apd.setProductUrl(retInSql.get("sPicUrl").toString());
			apd.setIsPspProduct((Boolean)retInSql.get("IsPspProduct"));
			if ((Boolean)retInSql.get("isNew")
						&&(YMTDateUtil.getDate().after((Date)retInSql.get("dListingTime")))
						&&(YMTDateUtil.getDate().before((Date)retInSql.get("dNewEndtime")))		){
				apd.setIsNew(true);
			}else{
				apd.setIsNew(false);
			}
			
			apd.setStockNum(catalogsInSql.stream().mapToInt(x->(Integer)x.get("iNum")).sum());
			apd.setIsPreSale(catalogsInSql.stream().anyMatch(x->(Boolean)x.get("IsPreSale")));
			apd.setMinPrice(catalogsInSql.stream().mapToDouble(x->new BigDecimal(x.get("fQuotePrice").toString()).doubleValue()).min().orElse(0));
			apd.setMaxPrice(catalogsInSql.stream().mapToDouble(x->new BigDecimal(x.get("fQuotePrice").toString()).doubleValue()).max().orElse(0));
			productsDto.add(apd);
		}
		retBean.setProducts(productsDto);
		return retBean;
				
		
	}

	
	
}
